import express from "express";
import { formatResponse } from "../../utils/tools";
// import CatteryModel from "../../dao/model/pet/CatteryModel";
import CatModel from "../../dao/model/pet/CatModel";
import CatteryModel from "../../dao/model/pet/CatteryModel";
const router = express.Router();

router.get("/", async (_req: any, res: any) => {

  let data = await CatModel.findAll();
  data = data.map(item => item.dataValues)

  data.forEach(async (item: any) => {
    if (!item.homeName || item.catteryId) {
      return;
    }
    const cattery = await CatteryModel.findOne({
      where: {
        name: item.homeName
      }
    })

    item.catteryId = cattery.id
    await CatModel.update(item, {
      where: {
        id: item.id
      }
    })
  })

  res.send(formatResponse(data.length));
});

export default router;